package interview.data_structure;

import java.util.Arrays;

/**
 * @author fance
 * @date 2018/7/14 15:34
 */
public class MyStack<E> {
    private Object[] stack;
    private int size;

    public MyStack() {
        stack = new Object[10];
    }
    public boolean isEmpty() {
        return size == 0;
    }

    public E peek() {
        if (isEmpty()) {
            return null;
        }
        return (E) stack[size - 1];
    }

    public E pop() {
        E e = peek();
        stack[size - 1] = null;
        size--;
        return e;
    }

    public void push(E item) {
        ensureCapacity(size + 1);
        stack[size++] = item;
    }

    public void ensureCapacity(int size) {
        int len = stack.length;
        if (size > len) {
            int newLen = size * 2;
            stack = Arrays.copyOf(stack,newLen);
        }
    }

    public static void main(String[] args) {
        MyStack<Integer> stack = new MyStack<>();
        for (int i = 0; i < 100; i++) {
            stack.push(i);
        }

        for (int i = 0; i < 50; i++) {
            System.out.println(stack.pop());
        }
    }
}
